﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class ScWorkModelController : BaseController
    {
        private readonly ScWorkModelApp _scWorkModelApp;

        public ScWorkModelController(ScWorkModelApp scWorkModelApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._scWorkModelApp = scWorkModelApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult ScWorkModelAsync()
        {
            //await AddTracesInfo();
            return View();
        }


        public async Task<JsonResult> GetAllScWorkModelAsync()
        {
            var scWorkModels = await _scWorkModelApp.GetScWorkModelInfoDto();

            var pageData = new TableData<ScWorkModelInfoDto>(Draw, scWorkModels.Count, scWorkModels.Count, scWorkModels);
            //await AddTracesInfo(5, 0, "获取堆垛机作业模式数据");
            return Json(pageData);
        }

        public async Task<JsonResult> ModifyScLockStatusAsync()
        {
            var id = Request.Form["id"].ToInt64();
            var isLock = Request.Form["isLock"].ToBool();
            var scWorkModelDto = await _scWorkModelApp.GetScWorkModelInfoById(id);
            bool result;
            string msg = "锁定";
            if (isLock)
            {
                msg = "解锁";
            }
            if (scWorkModelDto == null) 
            { 
                result = false;
                msg += "失败！没有此堆垛机！";
                return CreateJsonGetResult(result, msg); 
            }
            scWorkModelDto.IsLock = !isLock;
            try
            {
                result = await _scWorkModelApp.UpdateScWorkModelInfo(scWorkModelDto);
                msg += "成功!";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            return CreateJsonGetResult(result, msg);
        }
        /// <summary>
        /// 添加或者编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> ScWorkModelAddOrEditAsync(long id)
        {
            var scWorkModelPageData = new ScWorkModelPageData();
            //调度类型下拉选择
            Dictionary<string, object> dropDic = EnumHelper.EnumListDic<EnumScWorkModel>("", "");
            scWorkModelPageData.ModelTypes = new List<ScWorkModelTypePageData>();
            if (dropDic != null)
            {
                foreach (var item in dropDic)
                {
                    scWorkModelPageData.ModelTypes.Add(new ScWorkModelTypePageData 
                    { 
                        ID = item.Value.ToInt32(), 
                        TypeName = item.Key.ToString() 
                    });
                }
            }
            if(id > 0)
            {
                var scWorkModelInfo = await _scWorkModelApp.GetScWorkModelInfoById(id);
                if (scWorkModelInfo != null)
                {
                    scWorkModelPageData.ID = scWorkModelInfo.ID;
                    scWorkModelPageData.ScEquipmentCode = scWorkModelInfo.ScEquipmentCode;
                    scWorkModelPageData.IsLock = scWorkModelInfo.IsLock;
                    scWorkModelPageData.WorkModel = scWorkModelInfo.WorkModel;
                }
            }
            return View(scWorkModelPageData);
        }

        /// <summary>
        /// 数据操作添加或者编辑
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateScWorkModelInfoAsync()
        {
            var scWorkModelDto = new ScWorkModelInfoDto();
            scWorkModelDto.ID = Request.Form["id"].ToInt64();
            scWorkModelDto.ScEquipmentCode = Request.Form["ScEquipmentCode"].ToString();
            scWorkModelDto.IsLock = Request.Form["IsLock"].ToBool();
            scWorkModelDto.WorkModel = Request.Form["WorkModel"].ToInt32();

            bool result;
            string msg;
            try
            {
                if (scWorkModelDto.ID == 0)
                {
                    result = await _scWorkModelApp.CreateScWorkModelInfoAsync(scWorkModelDto);

                    await AddTracesInfo(2, 0, "新增堆垛机作业模式配置");
                }
                else
                {
                    result = await _scWorkModelApp.UpdateScWorkModelInfo(scWorkModelDto);
                    await AddTracesInfo(3, 0, "修改堆垛机作业模式配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            return CreateJsonGetResult(result, msg);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DeleteScWorkModelInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _scWorkModelApp.DeleteScWorkModelInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除堆垛机作业模式配置");
            return CreateJsonGetResult(result, msg);
        }
     }
}
